// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env:"cloud1-3g1yerk2e342b1a8",
})

// 云函数入口函数
exports.main = async (event, context) => {
  console.log("666",event)
  const wxContext = cloud.getWXContext()
  const db=cloud.database();

  const {userInfo}=event;
  userInfo.openid=wxContext.OPENID;
  var check = await db.collection("UserInfos").where({openid:wxContext.OPENID}).count()
  if(check.total == 1){
    await db.collection('UserInfos').where({
      openid:wxContext.OPENID
    }).update({
      data: userInfo
    })
  }
  else{
    await db.collection('UserInfos').add({
      data: userInfo
    })
  }
}